<template>
	<view class="container">
		<view class="form">
			<text class="title">登录</text>
			<input class="input" v-model="loginVal" placeholder="请输入用户名/手机号" />
			<input class="input" v-model="password" type="password" placeholder="请输入密码" password="true" />
			<button class="btn" type="primary" @click="handleLogin">登录</button>
			<view class="link">
				<text>没有账号？</text>
				<navigator url="/pages/tabBar/tabcompage/register/register">去注册</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginVal: '',
				password: ''
			}
		},
		methods: {
			async handleLogin() {
				console.log("[登录页][handleLogin] 开始执行登录，参数：", {
					loginVal: this.loginVal,
					password: "******"
				});

				if (!this.loginVal.trim() || !this.password.trim()) {
					uni.showToast({
						title: '请输入完整信息',
						icon: 'none',
						duration: 1500
					});
					return;
				}

				uni.showLoading({
					title: '登录中...',
					mask: true
				});

				try {
					const res = await uniCloud.callFunction({
						name: 'login',
						data: {
							loginVal: this.loginVal.trim(),
							password: this.password.trim()
						}
					});

					uni.hideLoading();
					console.log("[登录页][handleLogin] 云函数返回结果：", res);

					if (res.result && res.result.code === 200) {
						uni.setStorageSync("isLogin", true);
						uni.setStorageSync("userInfo", res.result.data); // 存储完整用户信息
						uni.showToast({
							title: '登录成功',
							icon: 'success',
							duration: 1500
						});

						setTimeout(() => {
							uni.switchTab({
								url: "/pages/UserInfoPage/UserInfoPage",
								success: () => {
									console.log("[登录页] 跳转成功！");
									uni.setTabBarItem({
										index: 3,
										text: "我的"
									});
								},
								fail: (err) => console.error("[登录页] 跳转失败：", err)
							});
						}, 1500);
					} else {
						uni.showToast({
							title: res.result?.msg || '账号/手机号或密码错误',
							icon: 'none',
							duration: 1500
						});
					}
				} catch (err) {
					uni.hideLoading();
					console.error("[登录页][handleLogin] 异常：", err);
					uni.showToast({
						title: '网络异常，请重试',
						icon: 'none',
						duration: 1500
					});
				}
			}
		}
	}
</script>

<style scoped>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 40rpx;
		box-sizing: border-box;
	}

	.form {
		width: 100%;
		max-width: 600rpx;
		background: white;
		padding: 60rpx 40rpx;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	.title {
		display: block;
		text-align: center;
		font-size: 48rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 60rpx;
	}

	.input {
		width: 100%;
		height: 80rpx;
		border: 2rpx solid #e0e0e0;
		border-radius: 10rpx;
		padding: 0 20rpx;
		margin-bottom: 40rpx;
		font-size: 28rpx;
		box-sizing: border-box;
	}

	.input:focus {
		border-color: #007AFF;
		outline: none;
	}

	.btn {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 10rpx;
		font-size: 32rpx;
		margin-bottom: 40rpx;
	}

	.link {
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		color: #666;
	}

	.link navigator {
		color: #007AFF;
		margin-left: 10rpx;
		text-decoration: none;
	}
</style>